package com.google.code.mochaccino.framework.crypto.pgp.impl;

/*
 * Copyright 2012 Claude Houle claude.houle@gmail.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import com.google.code.mochaccino.framework.crypto.EncryptionException;
import com.google.code.mochaccino.framework.crypto.EncryptionProvider;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.security.NoSuchProviderException;
import java.util.Iterator;
import org.apache.commons.io.IOUtils;
import org.bouncycastle.bcpg.ArmoredInputStream;
import org.bouncycastle.openpgp.PGPException;
import org.bouncycastle.openpgp.PGPPrivateKey;
import org.bouncycastle.openpgp.PGPPublicKey;
import org.bouncycastle.openpgp.PGPSecretKey;
import org.bouncycastle.openpgp.PGPSecretKeyRingCollection;

/** PGP Key Provider based on PGP-Blocks. */
public class PgpKeyProviderBlock implements PgpKeyProvider {

	/** The Private Key PGP */
	private String privateBlock;

	/** The Bouncy Castle representing the Private Key */
	private PGPPrivateKey privateKey;

	/** The Public Key PGP Block */
	private String publicBlock;

	/** The Bouncy Castle Object representing the Public Key */
	private PGPPublicKey publicKey;

	/** Constructor */
	public PgpKeyProviderBlock( String passphrase, String privateBlock, String publicBlock ) throws EncryptionException {
		this.privateBlock = privateBlock;
		this.publicBlock = publicBlock;
		try {
			ArmoredInputStream privIn = new ArmoredInputStream( new ByteArrayInputStream( privateBlock.getBytes() ) );
			PGPSecretKeyRingCollection privKeyCol = new PGPSecretKeyRingCollection( privIn );
			IOUtils.closeQuietly( privIn );
			// Extract Private SubKey
			Iterator<?> it = privKeyCol.getKeyRings();
			while ( it.hasNext() ) {
				PGPSecretKey sk = (PGPSecretKey) it.next();
				if ( sk.getPublicKey().isEncryptionKey() ) {
					privateKey = sk.extractPrivateKey( passphrase.toCharArray(), EncryptionProvider.getBouncyCastleName() );
					publicKey = sk.getPublicKey();
					break;
				}
			}
		} catch ( IOException e ) {
			throw new EncryptionException( e );
		} catch ( PGPException e ) {
			throw new EncryptionException( e );
		} catch ( NoSuchProviderException e ) {
			throw new EncryptionException( e );
		}
	}

	@Override
	public PGPPrivateKey getPrivateKey() {
		return privateKey;
	}

	@Override
	public String getPrivateKeyBlock() {
		return privateBlock;
	}

	@Override
	public PGPPublicKey getPublicKey() {
		return publicKey;
	}

	@Override
	public String getPublicKeyBlock() {
		return publicBlock;
	}
}
